%% runModelHelper_mm_v1_0
%  Version 1.0
%  Author: Adeyinka Lesi
%  Date: 11/10/20
%  Project: Tumor Growth, Logarithmic Continuum Form
%% Version History
%  1.0: testing Michaelis Menten rates

startt = clock();

conv_int = getLinearConverter(1000);
conv_area = getLinearConverter(100);
conv_vol = getConverter();
select1 = 1:7;
select2 = [1:2 4:8];
select3 = [2:8 10:11 13:14];
% select3 = [2 4 6:7 9:11 13:14 16:17];

save_name = 'ct10_lt015_ht04_021720_newact_mm_011421_ap';

pfems = [
    3.3111,0.78887,1159.4,0.45026,8.8984e-05,8.8984e-08,1,Inf,4.0875,0
    3.3111,0.78887,3.9956,6.1429e-06,8.8984e-05,8.8984e-08,1,Inf,4.0975,0
    3.3111,0.78887,5.3368,0.00080299,8.8984e-05,8.8984e-08,1,Inf,4.0401,0
    ];

pmals = [
    15.232,0.67368,3.6992e+05,0,7.8204e-05,7.8204e-08,0.99941,Inf,4.3256,0
    15.232,0.67368,14.734,2.7135e-05,7.8204e-05,7.8204e-08,0.99941,Inf,4.2539,0
    15.232,0.67368,3254.3,0.57179,7.8204e-05,7.8204e-08,0.99941,Inf,4.2395,0
    ];

mrs = cell(6,size(pfems,1));
run_list = [1:6];
if(~exist('objs_v1_10','var'))
    objs_v1_10 = zeros(6,size(pfems,1));
else
    if(size(pfems,1)>size(objs_v1_10,2))
        old_objs = objs_v1_10;
        objs_v1_10 = zeros(6,size(pfems,1));
        retain_list = setdiff(1:6,run_list);
        objs_v1_10(retain_list,1:size(old_objs,2)) = old_objs(retain_list,:);
    else
        objs_v1_10(run_list,1:size(pfems,1)) = 0;
    end
end

Nx = 500;
dt = 0.005;

min_surv = 2;
datafile1 = sprintf('ct10_lt015_ht04_021720_newact_s%i_011321/dist_RAD_zf2017_f_area_vs_count_over_actprim.csv',min_surv);
datafile2 = sprintf('ct10_lt015_ht04_021720_newact_s%i_011321/dist_NORAD_zf2017_f_area_vs_count_over_actprim.csv',min_surv);
datafile3 = sprintf('ct10_lt015_ht04_021720_newact_s%i_011321/dist_RAD_zf2017_m_area_vs_count_over_actprim.csv',min_surv);
datafile4 = sprintf('ct10_lt015_ht04_021720_newact_s%i_011321/dist_NORAD_zf2017_m_area_vs_count_over_actprim.csv',min_surv);
datafile5 = sprintf('ct10_lt015_ht04_021720_newact_s%i_011321/dist_NORAD_zf2019_f_area_vs_count_over_actprim.csv',min_surv);
datafile6 = sprintf('ct10_lt015_ht04_021720_newact_s%i_011321/dist_NORAD_zf2019_m_area_vs_count_over_actprim.csv',min_surv);

stage_sel = 1;
mm_noBack = struct('TIME_ZERO_CALCULATOR_DEPTH',1,'RATE_GENERATOR',@getRates_michaelisMenten_v1_0);
no_backtracking = struct('TIME_ZERO_CALCULATOR_DEPTH',1);
mm_args = struct('RATE_GENERATOR',@getRates_michaelisMenten_form11_v1_0);
mm_args2 = struct('RATE_GENERATOR',@getRates_michaelisMenten_form113_v1_0);
mm_args3 = struct('RATE_GENERATOR',@getRates_michaelisMenten_form123_v1_0);
null_args = struct();
args_sel = {null_args,mm_args,mm_args3};

for run_sel = run_list
    for i = 1:size(pfems,1)
        switch run_sel
            case 1
                pfem = pfems(i,:);
                params1 = pfem;
                params1([3:4 9:10]) = 0;
                [mrs{run_sel,i},data1,mg_fit1] = runModelFunction_v5_0(datafile1,params1,conv_vol,Nx,dt,select1,args_sel{i},stage_sel);
                mrs{run_sel,i}.data = data1;
                mrs{run_sel,i}.fit = mg_fit1;
                axis([10 3e7 0 max(data1.cdf(end,:))]);
                legend('Location','BestOutside');
                objs_v1_10(1,i) = timeWeightedObjective_v1_0(mg_fit1,data1.weights);
                plotMetaOriginTimes_v1_0(mrs{run_sel,i});
            case 2
                pfem = pfems(i,:);
                params2 = pfem;
                [mrs{run_sel,i},data2,mg_fit2] = runModelFunction_v5_0(datafile2,params2,conv_vol,Nx,dt,select2,args_sel{i},3);
                mrs{run_sel,i}.data = data2;
                mrs{run_sel,i}.fit = mg_fit2;
                axis([10 3e7 0 max(data2.cdf(end,:))]);
                legend('Location','BestOutside');
                objs_v1_10(2,i) = timeWeightedObjective_v1_0(mg_fit2,data2.weights);
            case 3
                pmal = pmals(i,:);
                params3 = pmal;
                params3([3:4 9:10]) = 0;
                [mrs{run_sel,i},data3,mg_fit3] = runModelFunction_v5_0(datafile3,params3,conv_vol,Nx,dt,select1,args_sel{i},stage_sel);
                mrs{run_sel,i}.data = data3;
                mrs{run_sel,i}.fit = mg_fit3;
                axis([10 3e7 0 max(data3.cdf(end,:))]);
                legend('Location','BestOutside');
                objs_v1_10(3,i) = timeWeightedObjective_v1_0(mg_fit3,data3.weights);
                plotMetaOriginTimes_v1_0(mrs{run_sel,i});
            case 4
                pmal = pmals(i,:);
                params4 = pmal;
                [mrs{run_sel,i},data4,mg_fit4] = runModelFunction_v5_0(datafile4,params4,conv_vol,Nx,dt,select2,args_sel{i},3);
                mrs{run_sel,i}.data = data4;
                mrs{run_sel,i}.fit = mg_fit4;
                axis([10 3e7 0 max(data4.cdf(end,:))]);
                legend('Location','BestOutside');
                objs_v1_10(4,i) = timeWeightedObjective_v1_0(mg_fit4,data4.weights);
            case 5
                pfem = pfems(i,:);
                params5 = pfem;
                [mrs{run_sel,i},data5,mg_fit5] = runModelFunction_v5_0(datafile5,params5,conv_vol,Nx,dt,select3,args_sel{i},3);
                mrs{run_sel,i}.data = data5;
                mrs{run_sel,i}.fit = mg_fit5;
                axis([10 3e7 0 max(data5.cdf(end,:))]);
                legend('Location','BestOutside');
                objs_v1_10(5,i) = timeWeightedObjective_v1_0(mg_fit5,data5.weights);
            case 6
                pmal = pmals(i,:);
                params6 = pmal;
                [mrs{run_sel,i},data6,mg_fit6] = runModelFunction_v5_0(datafile6,params6,conv_vol,Nx,dt,select3,args_sel{i},3);
                mrs{run_sel,i}.data = data6;
                mrs{run_sel,i}.fit = mg_fit6;
                axis([10 3e7 0 max(data6.cdf(end,:))]);
                legend('Location','BestOutside');
                objs_v1_10(6,i) = timeWeightedObjective_v1_0(mg_fit6,data6.weights);
        end
    end
end
save([save_name '_' mfilename],'mrs');

endt = clock();
duramins = etime(endt,startt)/60;

fprintf('%s took %3.2f minutes\n',mfilename,duramins);